package com.dmp.onlinestore.apps;

import java.util.List;

import com.dmp.onlinestore.model.dao.ProductDao;
import com.dmp.onlinestore.model.dao.base.DaoException;
import com.dmp.onlinestore.model.dao.base.DaoFactory;
import com.dmp.onlinestore.model.dao.base.DaoTransaction;
import com.dmp.onlinestore.model.dto.ProductBean;
import com.dmp.onlinestore.model.dto.ProductBeanFilterSpec;
//import com.dmp.onlinestore.model.service.Catalog;
import com.dmp.onlinestore.model.service.ProductBeanPager;
import com.dmp.onlinestore.model.service.base.ServiceFactory;
//import com.dmp.onlinestore.model.service.impl.CatalogImpl;
//import com.dmp.onlinestore.model.service.impl.ProductBeanPagerImpl;


public class TestPagingApp {

	public static void main(String[] args) {
		TestPagingApp app = new TestPagingApp();
		
		// Uncomment the test you want to run...
		app.pagerBasicIteration();
//		app.pagerOverRun();
//		app.pagerFilterByCategory();
//		app.pagerResetPageSize();
//		app.pagerGoToPage();

		app.testIndices();
	
	}
	
	// Prints a list of products.
	private void printProductList(List<ProductBean> productList) {
	    for (ProductBean prod : productList) {
	    	System.out.println("Product is: " + prod.toString());
	    }
	}
	
	// Test basic page iteration.
	public void pagerBasicIteration() {
		System.out.println("---------------------------------------------------------");
		System.out.println("Testcase: PagerBasicIteration");
		System.out.println("---------------------------------------------------------");
		try {
			DaoTransaction transaction = DaoFactory.instance().getDaoTransaction();
			ProductDao dao = DaoFactory.instance().getProductDao(transaction);
			List<ProductBean> productList = dao.getProducts(ProductBeanFilterSpec.getDefaultFilter());
			transaction.finish();
			ProductBeanPager pager = ServiceFactory.instance().getProductBeanPager(productList);
		    while (pager.hasMoreDownPages()) {
		    	System.out.println("Next Page...");
		        printProductList(pager.pageDown());
		    	System.out.println("-----------");
		    }
		    while (pager.hasMoreUpPages()) {
		    	System.out.println("Previous Page...");
		    	printProductList(pager.pageUp());
		    	System.out.println("-----------");
		    }
		}
		catch (DaoException e) {
			System.out.println("Dao exception: \"" + e.getErrorString() + "\"");
			e.printStackTrace();
		}
	} // pagerBasicIteration
	
	
	// Test overrunning the ends of the pager.
	public void pagerOverRun() {
		System.out.println("---------------------------------------------------------");
		System.out.println("Testcase: PagerOverRun");
		System.out.println("---------------------------------------------------------");
		try {
			DaoTransaction transaction = DaoFactory.instance().getDaoTransaction();
			ProductDao dao = DaoFactory.instance().getProductDao(transaction);
			List<ProductBean> productList = dao.getProducts(ProductBeanFilterSpec.getDefaultFilter());
			transaction.finish();
			ProductBeanPager pager = ServiceFactory.instance().getProductBeanPager(productList);
		    int numIterations = (pager.getManagedListSize() / pager.getPageSize()) + 3;
		    for (int i=0; i<numIterations; i++) {
		    	System.out.println("Next page...");
		    	printProductList(pager.pageDown());
		    	System.out.println("-----------");
		    }
		    for (int i=0; i<numIterations; i++) {
		    	System.out.println("Previous Page...");
		        printProductList(pager.pageUp());
		    	System.out.println("-----------");
		    }
		}
		catch (DaoException e) {
			System.out.println("Dao exception: \"" + e.getErrorString() + "\"");
			e.printStackTrace();
		}
	} // pagerOverRun

	
	// Test filtering by category.
	public void pagerFilterByCategory() {
		System.out.println("---------------------------------------------------------");
		System.out.println("Testcase: PagerFilterByCategory");
		System.out.println("---------------------------------------------------------");
		try {
			DaoTransaction transaction = DaoFactory.instance().getDaoTransaction();
			ProductDao dao = DaoFactory.instance().getProductDao(transaction);
			
			System.out.println("Non-filtered list (for reference)...");
			ProductBeanFilterSpec filter1 = ProductBeanFilterSpec.getDefaultFilter();
			List<ProductBean> productList1 = dao.getProducts(filter1);
			ProductBeanPager pager1 = ServiceFactory.instance().getProductBeanPager(productList1);
			pager1.setPageSize(pager1.getManagedListSize());  // just print all on one page
		    printProductList(pager1.pageDown());
		    
	    	System.out.println("-----------");
			System.out.println("Filtered list...");
		    ProductBeanFilterSpec filter2 = ProductBeanFilterSpec.getDefaultFilter();
		    filter2.setCatMatch("category-2");
		    //filter2.setSubcatMatch(3);
			List<ProductBean> productList2 = dao.getProducts(filter2);
			ProductBeanPager pager2 = ServiceFactory.instance().getProductBeanPager(productList2);
		    while (pager2.hasMoreDownPages()) {
		        printProductList(pager2.pageDown());
		    	System.out.println("-----------");
		    }
		    
			transaction.finish();
		}
		catch (DaoException e) {
			System.out.println("Dao exception: \"" + e.getErrorString() + "\"");
			e.printStackTrace();
		}
	} // pagerFilterByCategory

	
	// Test resetting the page size.
	public void pagerResetPageSize() {
		System.out.println("---------------------------------------------------------");
		System.out.println("Testcase: PagerResetPageSize");
		System.out.println("---------------------------------------------------------");
		try {
			DaoTransaction transaction = DaoFactory.instance().getDaoTransaction();
			ProductDao dao = DaoFactory.instance().getProductDao(transaction);
			List<ProductBean> productList = dao.getProducts(ProductBeanFilterSpec.getDefaultFilter());
			transaction.finish();
			
			ProductBeanPager pager = ServiceFactory.instance().getProductBeanPager(productList);
			
		    System.out.println("Paging down two pages...");
		    printProductList(pager.pageDown());
		    printProductList(pager.pageDown());
	
		    System.out.println("Bumping up page size by two...");
		    printProductList(pager.setPageSize(pager.getPageSize()+2));
		    
		    System.out.println("Page up one page...");
		    printProductList(pager.pageUp());
		}
		catch (DaoException e) {
			System.out.println("Dao exception: \"" + e.getErrorString() + "\"");
			e.printStackTrace();
		}
	} // pagerResetPageSize

	// Test going to a specific page.
	public void pagerGoToPage() {
		System.out.println("---------------------------------------------------------");
		System.out.println("Testcase: PagerGoToPage");
		System.out.println("---------------------------------------------------------");
		try {
			DaoTransaction transaction = DaoFactory.instance().getDaoTransaction();
			ProductDao dao = DaoFactory.instance().getProductDao(transaction);
			List<ProductBean> productList = dao.getProducts(ProductBeanFilterSpec.getDefaultFilter());
			transaction.finish();
			
			ProductBeanPager pager = ServiceFactory.instance().getProductBeanPager(productList);
		    int pageNum = 1;
		    while (pager.hasMoreDownPages()) {
		    	System.out.println("Page #" + pageNum++);
		        printProductList(pager.pageDown());
		    	System.out.println("-----------");
		    }
		    System.out.println("Going to page 2...");
		    printProductList(pager.goToPage(2));

		    System.out.println("Going to page 1...");
		    printProductList(pager.goToPage(1));

		    System.out.println("Going to page 3...");
		    printProductList(pager.goToPage(3));

		    System.out.println("Going to page 4...");
		    printProductList(pager.goToPage(4));

		    System.out.println("Going to page 0...");
		    printProductList(pager.goToPage(0));
		}
		catch (DaoException e) {
			System.out.println("Dao exception: \"" + e.getErrorString() + "\"");
			e.printStackTrace();
		}
	}
	
	public void testIndices() {
		System.out.println("---------------------------------------------------------");
		System.out.println("Testcase: TestIndices");
		System.out.println("---------------------------------------------------------");
		try {
			DaoTransaction transaction = DaoFactory.instance().getDaoTransaction();
			ProductDao dao = DaoFactory.instance().getProductDao(transaction);
			List<ProductBean> productList = dao.getProducts(ProductBeanFilterSpec.getDefaultFilter());
			transaction.finish();
			
			ProductBeanPager pager = ServiceFactory.instance().getProductBeanPager(productList);
			List<String> indicesList = pager.getPageIndices();
			System.out.println("List of indices");
		    for (int i=0 ; i < indicesList.size(); i++) {
		    	int pageNum = i+1;
		    	String range = indicesList.get(i);
		    	System.out.println("Page #" + pageNum + " is >>" + range + "<<");
		    }
		}
		catch (DaoException e) {
			System.out.println("Dao exception: \"" + e.getErrorString() + "\"");
			e.printStackTrace();
		}
		
	}
	

	
	
	
}

// Other ways of iterating...
//Iterator<ProductBean> it = null;
//
//// Skinning the cat this way...
//System.out.println("List (#1) of products...");
//it = products.iterator();
//while (it.hasNext()) {
//	ProductBean prod = it.next();
//	System.out.println("Product is: " + prod.toString());
//}
//
//// And now this way...
//System.out.println("List (#2) of products...");
//for (it = products.iterator(); it.hasNext();) {
//    ProductBean prod = it.next();
//    System.out.println("Product is: " + prod.toString());
//}

